/******************************************************************************
 * Copyright (C) 2016 Wuhan Water Elephant Co.Ltd All Rights Reserved. 
 * 本软件为武汉水象科技有限公司开发研制。 未经本公司正式书面同意，其他任何个人、
 * 团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package com.sxfq.loansupermarket.app.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.sxfq.loansupermarket.app.platform.service.PlatformService;
import com.sxfq.loansupermarket.business.base.SqlMapper;
import com.sxfq.loansupermarket.business.customer.entity.CustomerPlatformVisitInfo;
import com.sxfq.loansupermarket.business.customer.service.CustomerPlatformVisitInfoService;
import com.sxfq.loansupermarket.business.platform.service.PlatformProductInfoService;
import com.sxfq.loansupermarket.common.utils.DoubleUtil;
import com.sxfq.loansupermarket.common.utils.StringUtil;

/**
 * 
 * 
 * Module:
 * 
 * PlatFormServiceImpl.java
 * 
 * @author 程盼
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Service
public class PlatformServiceImpl implements PlatformService {

	private Logger logger = Logger.getLogger(PlatformServiceImpl.class);

	@Resource
	private SqlMapper sqlMapper;

	@Resource
	private PlatformProductInfoService platformProductInfoService;

	@Resource
	private CustomerPlatformVisitInfoService customerPlatformVisitInfoService;

	/**
	 * 查询首页产品
	 * 
	 * @see com.sxfq.loansupermarket.app.platform.service.PlatformService#findBwRepaymentPlanPage(com.sxfq.loansupermarket.business.dto.PageRequestVo)
	 */
	@Override
	public List<Map<String, Object>> getPlatformList(int pageNum, int pageSize) {
		String sql = "select b.abbreviation,b.logo,a.min_quato minQuato,a.max_quato maxQuato,a.id from platform_product_info a left join platform_company_info b on a.company_id=b.id where a.home_display=1 and a.status=1 and a.ifdel!=1 and b.ifdel!=1 order by a.sort desc limit "
				+ (pageNum - 1) * pageSize + "," + pageSize;
		logger.info("【PlatFormServiceImpl.getPlatformList】sql:" + sql);
		List<Map<String, Object>> platFormList = sqlMapper.selectList(sql);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (!CollectionUtils.isEmpty(platFormList)) {
			for (Map<String, Object> map : platFormList) {
				Map<String, Object> platFormMap = new HashMap<String, Object>();
				platFormMap.put("platformId", StringUtil.toString(map.get("id")));// 平台ID
				platFormMap.put("abbreviation", StringUtil.toString(map.get("abbreviation")));// 公司名称
				platFormMap.put("logo", StringUtil.toString(map.get("logo")));// logo
				platFormMap.put("quato",
						getQuato(StringUtil.toString(map.get("minQuato")), StringUtil.toString(map.get("maxQuato"))));// 放款额度
				platFormMap.put("label", getPlatformLabel(StringUtil.toString(map.get("id"))));// 标签
				list.add(platFormMap);
			}
		}
		return list;
	}

	/**
	 * 查询推荐产品
	 * 
	 * @see com.sxfq.loansupermarket.app.platform.service.PlatformService#findBwRepaymentPlanPage(com.sxfq.loansupermarket.business.dto.PageRequestVo)
	 */
	@Override
	public List<Map<String, Object>> getRecommendPlatformList(int pageNum, int pageSize) {
		String sql = "select b.abbreviation,b.logo,a.min_quato minQuato,a.max_quato maxQuato,a.id from platform_product_info a left join platform_company_info b on a.company_id=b.id  where a.status=1 and a.ifdel!=1 and b.ifdel!=1 order by a.sort desc limit "
				+ (pageNum - 1) * pageSize + "," + pageSize;
		logger.info("【PlatFormServiceImpl.getPlatformList】sql:" + sql);
		List<Map<String, Object>> platFormList = sqlMapper.selectList(sql);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (!CollectionUtils.isEmpty(platFormList)) {
			for (Map<String, Object> map : platFormList) {
				Map<String, Object> platFormMap = new HashMap<String, Object>();
				platFormMap.put("platformId", StringUtil.toString(map.get("id")));// 平台ID
				platFormMap.put("abbreviation", StringUtil.toString(map.get("abbreviation")));// 公司名称
				platFormMap.put("logo", StringUtil.toString(map.get("logo")));// logo
				platFormMap.put("quato",
						getQuato(StringUtil.toString(map.get("minQuato")), StringUtil.toString(map.get("maxQuato"))));// 放款额度
				platFormMap.put("label", getPlatformLabel(StringUtil.toString(map.get("id"))));// 标签
				list.add(platFormMap);
			}
		}
		return list;
	}

	/**
	 * 获取平台标签
	 * 
	 * @see com.sxfq.loansupermarket.app.platform.service.PlatformService#getPlatformLabel(java.lang.String)
	 */
	@Override
	public String getPlatformLabel(String platformId) {
		String labelSql = "select c.label_name labelName from platform_product_info a inner join platform_product_label_relation b on a.id=b.product_id left join platform_product_label c on b.label_id=c.id where a.id="
				+ platformId;
		List<Map<String, Object>> lableList = sqlMapper.selectList(labelSql);
		String label = "";
		if (!CollectionUtils.isEmpty(lableList)) {
			for (int i = 0; i < lableList.size(); i++) {
				label += StringUtil.toString(lableList.get(i).get("labelName")) + ",";
			}
		}
		return StringUtil.removeEnd(label, ",");
	}

	/**
	 * 查询平台详情
	 * 
	 * @see com.sxfq.loansupermarket.app.platform.service.PlatformService#getPlatformDetail(java.lang.String)
	 */
	@Override
	public Map<String, Object> getPlatformDetail(String platformId) {
		String sql = "select b.abbreviation,a.daily_interest_rate,a.product_img logo,a.advantage,a.min_quato minQuato,a.max_quato maxQuato,a.max_cycle maxCycle,a.min_cycle minCycle,a.lend_speed lendSpeed,a.apply_condition applyCondition,a.require_document requireDocument,a.audit_process auditProcess,a.id,a.link,a.brief_introduction introduction from platform_product_info a left join platform_company_info b on a.company_id=b.id where a.id="
				+ platformId + " limit 0,1";
		logger.info("【PlatFormServiceImpl.getPlatformDetail】sql:" + sql);
		Map<String, Object> map = sqlMapper.selectOne(sql);
		Map<String, Object> platMap = new HashMap<String, Object>();
		if (map != null) {
			platMap.put("abbreviation", StringUtil.toString(map.get("abbreviation")));// 公司名称
			platMap.put("logo", StringUtil.toString(map.get("logo")));// 产品图片
			platMap.put("advantage", StringUtil.toString(map.get("advantage")));// 产品优势
			platMap.put("label", getPlatformLabel(StringUtil.toString(map.get("id"))));// 平台标签
			platMap.put("quato",
					getQuato(StringUtil.toString(map.get("minQuato")), StringUtil.toString(map.get("maxQuato"))));// 放款额度
			platMap.put("maxCycle",
					getCycle((int)map.get("minCycle"),(int)map.get("maxCycle")) + "天");// 期限范围
			platMap.put("lendSpeed", StringUtil.toString(map.get("lendSpeed")) + "天");// 最快放款
			platMap.put("applyCondition", StringUtil.toString(map.get("applyCondition")));// 申请条件
			platMap.put("requireDocument", StringUtil.toString(map.get("requireDocument")));// 所需材料
			platMap.put("introduction", StringUtil.toString(map.get("introduction")));// 产品简介
			platMap.put("link", StringUtil.toString(map.get("link")));// 跳转链接
			platMap.put("dailyInterestRate", StringUtil.toString(map.get("daily_interest_rate")));// 日利率
		}
		return platMap;
	}

	private String getQuato(String minQuato, String maxQuato) {
		String quato = "";
		if (!StringUtil.isEmpty(minQuato)) {
			quato = DoubleUtil.toZeroDecimal(Double.valueOf(minQuato));
		}
		if (!StringUtil.isEmpty(maxQuato) &&!maxQuato.equals("0.00")) {
			quato = DoubleUtil.toZeroDecimal(Double.valueOf(maxQuato));
		}
		if (!StringUtil.isEmpty(minQuato) && !StringUtil.isEmpty(maxQuato)&&!maxQuato.equals("0.00")) {
			quato = DoubleUtil.toZeroDecimal(Double.valueOf(minQuato)) + "~"
					+ DoubleUtil.toZeroDecimal(Double.valueOf(maxQuato));
		}
		return quato;
	}

	private String getCycle(Integer minCycle, Integer maxCycle) {
		String Cycle = "";
		if (minCycle!=null) {
			Cycle+= minCycle;
		}
		if (maxCycle!=null) {
			Cycle+=maxCycle;
		}
		if (minCycle!=null&&maxCycle!=null) {
			Cycle = minCycle + "~"
					+ maxCycle;
		}
		return Cycle;
	}

	/**
	 * 是否首次点击
	 * 
	 * @see com.sxfq.loansupermarket.app.platform.service.PlatformService#getIsFirstClick(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public Map<String, Object> getIsFirstClick(String borrowerId, String platformId) {
		CustomerPlatformVisitInfo entity = new CustomerPlatformVisitInfo();
		entity.setBorrowerId(Long.parseLong(borrowerId));
		entity.setPlatformId(Long.parseLong(platformId));
		int i = customerPlatformVisitInfoService.selectCount(entity);
		Map<String, Object> map = new HashMap<String, Object>();
		if (i > 0) {
			map.put("isFirst", "0");// 不是第一次
		} else {
			map.put("isFirst", "1");// 是第一次
		}
		return map;
	}

}
